from htglxt import models, serhtglxt
from rest_framework.views import APIView
from rest_framework.response import Response


class LoginView(APIView):
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = models.User.objects.filter(username=username, password=password).first()
        admin = models.Admin.objects.filter(username=username, password=password).first()
        if admin:
            ser = serhtglxt.AdminSerializer(admin, many=False)
            return Response({'code': 200, 'msg': '登录成功', 'data': ser.data, 'role': 'super_admin'})
        elif user:
            ser = serhtglxt.UserSerializer(user, many=False)
            return Response({'code': 200, 'msg': '登录成功', 'data': ser.data, 'role': 'admin'})
        else:
            return Response({'code': 400, 'msg': '用户名或密码错误'})



